package com.ftg.learn.framework;

import java.util.ArrayList;
import java.util.List;

/**
 * 初始化
 *
 * @author kangjx
 * @version 1.0
 * @date 2023/9/18 下午2:46
 */
public class TreeInit {

    private DeptTree findTop(List<Dept> rows, int pid) {
        DeptTree deptTree = new DeptTree();
        for (int i = 0; i < rows.size(); i++) {
            if (rows.get(i).getPid() == pid) {
                deptTree.setId(rows.get(i).getId());
                deptTree.setName(rows.get(i).getName());
                deptTree.setLoc(rows.get(i).getLoc());
                deptTree.setPid(pid);

                //rows.get(i).getId() 要做为其它pid存在
                List<DeptTree> children = new ArrayList<>();
                for (Dept row : rows) {
                    if (row.getPid() == rows.get(i).getId()) {

                        DeptTree son = new DeptTree();
                        son.setId(row.getId());
                        son.setName(row.getName());
                        son.setLoc(row.getLoc());
                        son.setPid(row.getPid());
                        children.add(son);


                    }
                }
                deptTree.setChildren(children);
            }
        }
        return deptTree;
    }


    private DeptTree findParent(List<Dept> rows, int pid) {
        DeptTree deptTree = new DeptTree();
        for (int i = 0; i < rows.size(); i++) {
            if (rows.get(i).getPid() == pid) {
                deptTree.setId(rows.get(i).getId());
                deptTree.setName(rows.get(i).getName());
                deptTree.setLoc(rows.get(i).getLoc());
                deptTree.setPid(pid);

                findSon(deptTree,rows,rows.get(i).getId());

            }
        }
        return deptTree;
    }

    private void findSon(DeptTree deptTree,List<Dept> rows, int pid) {
        for (Dept row : rows) {
            if(row.getPid() == pid){
                DeptTree son = new DeptTree();
                son.setId(row.getId());
                son.setPid(row.getPid());
                son.setName(row.getName());
                son.setLoc(row.getLoc());
                deptTree.getChildren().add(son);
                findSon(son,rows,row.getId());
            }
        }
    }


    public static void main(String[] args) {

        List<Dept> list = new ArrayList<>();

        Dept dept = new Dept();
        dept.setId(1);
        dept.setPid(0);
        dept.setName("前沿集团");
        dept.setLoc("哈尔滨");
        list.add(dept);

        Dept dept1 = new Dept();
        dept1.setId(2);
        dept1.setPid(1);
        dept1.setName("江北分公司");
        dept1.setLoc("哈尔滨");
        list.add(dept1);

        Dept dept2 = new Dept();
        dept2.setId(3);
        dept2.setPid(2);
        dept2.setName("软件研发一部");
        dept2.setLoc("哈尔滨");
        list.add(dept2);

        Dept dept3 = new Dept();
        dept3.setId(4);
        dept3.setPid(2);
        dept3.setName("UI一部");
        dept3.setLoc("哈尔滨");
        list.add(dept3);

        Dept dept4 = new Dept();
        dept4.setId(5);
        dept4.setPid(1);
        dept4.setName("江南分公司");
        dept4.setLoc("哈尔滨");
        list.add(dept4);

        TreeInit t = new TreeInit();
        System.out.println(t.findParent(list, 0));


    }



}
